#!/bin/sh

# ## 使用方法：
# source ~/cscope.sh [-d|-c|-u] dir
#
# ```
# > cscope.sh脚本同时支持多种命令
# > -d 命令后跟具体的项目代码目录，可以在该目录生成对应的cscope数据库
# > -c 切换数据库，方便有多个项目间切换
# > -u 或者不带参数, 更新cscope数据库
#
# ```
# ## 简化方法：
#
# 1. 在~/.bashrc中添加环境变量：export cs="source ~/cscope.sh"
# 2. 执行$cs等于执行“source ~/cscope.sh”命令, 如：$cs -d ~/work/project_xx

# Use colors, but only if connected to a terminal
# and that terminal supports them.

# The [ -t 1 ] check only works when the function is not called from
# a subshell (like in `$(...)` or `(...)`, so this hack redefines the
# function at the top level to always return false when stdout is not
# a tty.
if [ -t 1 ]; then
  is_tty() {
    true
  }
else
  is_tty() {
    false
  }
fi

if is_tty; then
    RED=$(printf '\033[31m')
    GREEN=$(printf '\033[32m')
    YELLOW=$(printf '\033[33m')
    BLUE=$(printf '\033[34m')
    DARKGREEN=$(printf '\036[38m')
    WHITE=$(printf '\033[37m')
    BLINK=$(printf '\033[5m')
    BOLD=$(printf '\033[1m')
    RESET=$(printf '\033[0m')
fi

SRC_PATH=`pwd`

update=1 #default for update
change=0

printf "${BLUE}%s${RESET}\n" $SHELL

set source path
case $1 in
    -d)
        SRC_PATH=$2
        printf "${BLUE}%s${RED}%s${RESET}\n" "set source path: " ${SRC_PATH}
        ;;
    -c)
        SRC_PATH=$2
        change=1
        printf "${BLUE}%s${RED}%s${RESET}\n" "change source path: " ${SRC_PATH}
        ;;
    -u|*)
        update=1
        printf "${BLUE}%s${RED}%s${RESET}\n" "update source path: " ${SRC_PATH}
esac

cd ${SRC_PATH}
printf "${YELLOW}%s${RESET}\n" "# switch to $SRC_PATH"

if [ 1 -eq ${change} ]; then
    printf "${YELLOW}%s${RESET}\n" "# change project cscope database!"
    res=$(find ${SRC_PATH} -name cscope.out)

    if [ "x"${res} = "x" ]; then
        # printf "${YELLOW}%s${RESET}\n" "# not found cscope database, create cscope.files..."
        # find ${SRC_PATH} -name "*.h" -o -name "*.c" -o -name "*.cc" > cscope.files

        printf "${YELLOW}%s${RESET}\n" "# create cscope.out..."
        # cscope -bkq -i cscope.files
        cscope -bRq

        printf "${YELLOW}%s${RESET}\n" "# create tags..."
        # ctags -R *
        ctags -R --C-kinds=+p --fields=+aS --extra=+q
    else
        printf "${BLUE}%s${YELLOW}%s${RESET}\n" "found cscope database:${res}" "just change work path"
    fi
elif [ 1 -eq ${update} ]; then
    # printf "${YELLOW}%s${RESET}\n" "# update cscope.files..."
    # find ${SRC_PATH} -name "*.h" -o -name "*.c" -o -name "*.cc" > cscope.files

    printf "${YELLOW}%s${RESET}\n" "# update cscope database..."
    # cscope -bkq -i cscope.files
    cscope -bRq

    printf "${YELLOW}%s${RESET}\n" "# create tags..."
    ctags -R --C-kinds=+p --fields=+aS --extra=+q

    export CSCOPE_DB=${SRC_PATH}/cscope.out
fi

printf "${GREEN}%s${RESET}\n" "# done!"
printf '\n'

#printf '%s    __                       %s              ___          %s   __ %s\n' ${RED} ${GREEN} ${BLUE} $RESET
#printf '%s   / /  ___ ____  ___  __ __ %s _______  ___/ (_)__  ___ _%s  / / %s\n' ${RED} ${GREEN} ${BLUE} $RESET
#printf '%s  / _ \/ _ `/ _ \/ _ \/ // / %s/ __/ _ \/ _  / / _ \/ _ `/%s /_/  %s\n' ${RED} ${GREEN} ${BLUE} $RESET
#printf '%s /_//_/\_,_/ .__/ .__/\_, /  %s\__/\___/\_,_/_/_//_/\_, / %s(_)   %s\n' ${RED} ${GREEN} ${BLUE} $RESET
#printf '%s          /_/  /_/   /___/   %s                    /___/  %s      %s\n' ${RED} ${GREEN} ${BLUE} $RESET
#printf '\n'
#
#
#printf '%s ██░ ██  ▄▄▄       ██▓███   ██▓███ ▓██   ██▓  %s  ▄████▄   ▒█████  ▓█████▄ ██▓ ███▄    █   ▄████    %s  ▐██▌  %s\n' ${WHITE} ${GREEN} ${BLUE} $RESET
#printf '%s▓██░ ██▒▒████▄    ▓██░  ██▒▓██░  ██▒▒██  ██▒  %s ▒██▀ ▀█  ▒██▒  ██▒▒██▀ ██▌▓██▒ ██ ▀█   █  ██▒ ▀█▒  %s  ▐██▌  %s\n' ${WHITE} ${GREEN} ${BLUE} $RESET
#printf '%s▒██▀▀██░▒██  ▀█▄  ▓██░ ██▓▒▓██░ ██▓▒ ▒██ ██░  %s ▒▓█    ▄ ▒██░  ██▒░██   █▌▒██▒▓██  ▀█ ██▒▒██░▄▄▄░  %s  ▐██▌  %s\n' ${WHITE} ${GREEN} ${BLUE} $RESET
#printf '%s░▓█ ░██ ░██▄▄▄▄██ ▒██▄█▓▒ ▒▒██▄█▓▒ ▒ ░ ▐██▓░  %s ▒▓▓▄ ▄██▒▒██   ██░░▓█▄   ▌░██░▓██▒  ▐▌██▒░▓█  ██▓  %s  ▓██▒  %s\n' ${WHITE} ${GREEN} ${BLUE} $RESET
#printf '%s░▓█▒░██▓ ▓█   ▓██▒▒██▒ ░  ░▒██▒ ░  ░ ░ ██▒▓░  %s ▒ ▓███▀ ░░ ████▓▒░░▒████▓ ░██░▒██░   ▓██░░▒▓███▀▒  %s  ▒▄▄   %s\n' ${WHITE} ${GREEN} ${BLUE} $RESET
#printf '%s ▒ ░░▒░▒ ▒▒   ▓▒█░▒▓▒░ ░  ░▒▓▒░ ░  ░  ██▒▒▒   %s ░ ░▒ ▒  ░░ ▒░▒░▒░  ▒▒▓  ▒ ░▓  ░ ▒░   ▒ ▒  ░▒   ▒   %s  ░▀▀▒  %s\n' ${WHITE} ${GREEN} ${BLUE} $RESET
#printf '%s ▒ ░▒░ ░  ▒   ▒▒ ░░▒ ░     ░▒ ░     ▓██ ░▒░   %s   ░  ▒     ░ ▒ ▒░  ░ ▒  ▒  ▒ ░░ ░░   ░ ▒░  ░   ░   %s  ░  ░  %s\n' ${WHITE} ${GREEN} ${BLUE} $RESET
#printf '%s ░  ░░ ░  ░   ▒   ░░       ░░       ▒ ▒ ░░    %s ░        ░ ░ ░ ▒   ░ ░  ░  ▒ ░   ░   ░ ░ ░ ░   ░   %s     ░  %s\n' ${WHITE} ${GREEN} ${BLUE} $RESET
#printf '%s ░  ░  ░      ░  ░                  ░ ░       %s ░ ░          ░ ░     ░     ░           ░       ░   %s  ░     %s\n' ${WHITE} ${GREEN} ${BLUE} $RESET
#printf '%s                                    ░ ░       %s ░                  ░                               %s        %s\n' ${WHITE} ${GREEN} ${BLUE} $RESET
#printf '\n'

# printf '%s  |                                 %s                   | _)               %s   | %s\n' ${RED} ${GREEN} ${WHITE} $RESET
# printf '%s  __ \    _` |  __ \   __ \   |   | %s   __|   _ \    _` |  |  __ \    _` | %s   | %s\n' ${RED} ${GREEN} ${WHITE} $RESET
# printf '%s  | | |  (   |  |   |  |   |  |   | %s  (     (   |  (   |  |  |   |  (   | %s  _| %s\n' ${RED} ${GREEN} ${WHITE} $RESET
# printf '%s _| |_| \__,_|  .__/   .__/  \__, | %s \___| \___/  \__,_| _| _|  _| \__, | %s  _) %s\n' ${RED} ${GREEN} ${WHITE} $RESET
# printf '%s               _|     _|     ____/  %s                               |___/  %s     %s\n' ${RED} ${GREEN} ${WHITE} $RESET
# printf '\n'

printf '%s  |                            %s                 | _)              %s   | %s\n' ${RED} ${GREEN} ${WHITE} $RESET
printf '%s    \    _` |  _ \  _ \  |  |  %s   _|   _ \   _` |  |    \    _` | %s  _| %s\n' ${RED} ${GREEN} ${WHITE} $RESET
printf '%s _| _| \__,_| .__/ .__/ \_, |  %s \__| \___/ \__,_| _| _| _| \__, | %s  _) %s\n' ${RED} ${GREEN} ${WHITE} $RESET
printf '%s             _|   _|    ___/   %s                            ____/  %s     %s\n' ${RED} ${GREEN} ${WHITE} $RESET
printf '%s                               %s                                   %s     %s\n' ${RED} ${GREEN} ${WHITE} $RESET
printf '\n'



